using System;
using System.Collections;
using I2.Loc;
using UnityEngine;
using UnityEngine.SceneManagement;

public class controllerLoadingGame : MonoBehaviour
{
	private const int tutorialMaxCount = 3;

	private const string tutorialSaveKey = "WatchTutorialScreen";

	public static TypeGame typeGame;

	public UISprite sprite;

	public UISprite sprite2;

	public UITexture background;

	public UILabel hintLable;

	public GameObject hintContainer;

	public GameObject tutorialObject;

	public string normalTextureName;

	private int currentCountWatchTutorial;

	private string[] hints = new string[15]
	{
		"Lobby_to_Game_podskazki_1", "Lobby_to_Game_podskazki_2", "Lobby_to_Game_podskazki_3", "Lobby_to_Game_podskazki_4", "Lobby_to_Game_podskazki_5", "Lobby_to_Game_podskazki_6", "Lobby_to_Game_podskazki_7", "Lobby_to_Game_podskazki_8", "Lobby_to_Game_podskazki_9", "Lobby_to_Game_podskazki_10",
		"Lobby_to_Game_podskazki_11", "Lobby_to_Game_podskazki_12", "Lobby_to_Game_podskazki_13", "Lobby_to_Game_podskazki_14", "Lobby_to_Game_podskazki_15"
	};

	private void Awake()
	{
		SetBackgroundTexture();
		tutorialObject.SetActive(false);
		if ((bool)background && (bool)hintContainer && (bool)hintLable)
		{
			hintContainer.SetActive(true);
			hintLable.text = ScriptLocalization.Get("Loading_Screens/" + hints[UnityEngine.Random.Range(0, hints.Length)]);
		}
		sprite.fillAmount = 0f;
		sprite2.fillAmount = 0f;
	}

	private IEnumerator Start()
	{
		Save.SaveBool("keyTutorialActive", true);
		yield return null;
		string namescene = settings.keySelectScene;
		BCWDebug.Log("Destination scene: " + namescene);
		bool weakDev2 = false;
		weakDev2 = !QualityController.LightEnable;
		BCWStorage.SaveAll(true);
		GC.Collect();
		Resources.UnloadUnusedAssets();
		int length = BCWStorage.lengthStorage;
		float loadFillByFrame = 0.12f / (float)BCWStorage.lengthStorage;
		while (!SaveBCWStorage.canOpenNextLevel && sprite.fillAmount < 0.9f)
		{
			sprite.fillAmount += loadFillByFrame;
			sprite2.fillAmount = sprite.fillAmount;
			yield return null;
		}
		AsyncOperation load = SceneManager.LoadSceneAsync(namescene);
		while (!load.isDone)
		{
			float tmp = load.progress * 100f / 125f + 0.12f;
			if (tmp > sprite.fillAmount)
			{
				sprite.fillAmount = tmp;
			}
			sprite2.fillAmount = sprite.fillAmount;
			yield return null;
		}
		GC.Collect();
		Resources.UnloadUnusedAssets();
	}

	private void SetBackgroundTexture()
	{
		string text = typeGame.ToString().ToLower();
		if (typeGame == TypeGame.normal)
		{
		}
		text = BCWStorage.Get<string>(settings.keyForShowLoadingSprite);
		if (typeGame == TypeGame.normal)
		{
			text = "fonMenu_2";
			BCWStorage.Set(settings.keyForShowLoadingSprite, text);
		}
		Texture texture = Resources.Load<Texture>("TextureForPreload/loaders/" + text);
		if (texture != null && background != null)
		{
			background.mainTexture = texture;
		}
	}
}
